package parser;

import java.io.IOException;
import org.pdfbox.exceptions.CryptographyException;
import org.pdfbox.exceptions.InvalidPasswordException;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;

public class Pdf {
	
	private static Pdf instance;
	
    PDDocument document = null;
    PDFTextStripper stripper = null;
                
    private Pdf(){  }
    
    public static  Pdf getInstance() { if (instance == null) instance = new Pdf() ; return instance; }
    
    public String parseFile(String filePath) throws IOException, CryptographyException, InvalidPasswordException {
    	
    	String docContent = "";
    	
        try {
        	
            document = PDDocument.load(filePath);
            if( document.isEncrypted() ){
                    document.decrypt("-password");
            }                        
            stripper = new PDFTextStripper();
            docContent = stripper.getText(document);
            
        } catch (Exception ex) {
        	
            docContent = "";
            
        } finally {
        	
            try { document.close(); } 
            catch (Exception e ) { docContent = ""; }  
            
        }
        
        return docContent;
        
    }

}
